/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.HangSanXuatDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.HangSanXuat;

/**
 *
 * @author LaiHuynh
 */
public class XoaCapNhatHangsx extends javax.swing.JInternalFrame {

    private DefaultTableModel dtm;
    /**
     * Creates new form XoaCapNhatHangsx
     */
    public XoaCapNhatHangsx() {
        initComponents();
    }
    
    public void timKiem() {
        try {            
            this.dtm = new DefaultTableModel();
            String tenHang = this.jtfTenHangsx.getText();
            List<HangSanXuat> ds=HangSanXuatDAO.timKiem(tenHang);
            this.dtm.addColumn("Mã hãng sx");
            this.dtm.addColumn("Ten hãng sx");
            for(int i=0; i<ds.size(); i++){ 
                HangSanXuat sp=ds.get(i); 
                Vector vt = new Vector();
                vt.add(sp.getMaHangsx());
                vt.add(sp.getTenHang());
                this.dtm.addRow(vt);
            } 
            this.jtDanhSach.setModel(dtm);
            //chọn dòng đầu tiên trong Jtable
            if(this.jtDanhSach.getRowCount() > 0) {
                this.jtDanhSach.changeSelection(0, 0, false, false);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    public void xoaHangsx() {
        try {
            int dong = this.jtDanhSach.getSelectedRow();
            int maHangsx = (Integer) this.jtDanhSach.getValueAt(dong, 0);
            boolean kq = HangSanXuatDAO.xoaHangsx(maHangsx);
            if(kq == true) {
                JOptionPane.showMessageDialog(this, "Xóa thành công");
            } else {
                JOptionPane.showMessageDialog(this, "Xóa thất bại");
            }
            //Hien thi lai danh hang san xuat
            this.timKiem();
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    
    public void hienThiTatCaHangsx() {
        try {
            this.dtm = new DefaultTableModel();
            List<HangSanXuat> ds=HangSanXuatDAO.layDanhSachHangSanXuat();
            this.dtm.addColumn("Mã hãng sx");
            this.dtm.addColumn("Ten hãng sx");
            for(int i=0; i<ds.size(); i++){ 
                HangSanXuat sp=ds.get(i); 
                Vector vt = new Vector();
                vt.add(sp.getMaHangsx());
                vt.add(sp.getTenHang());
                this.dtm.addRow(vt);
            } 
            this.jtDanhSach.setModel(dtm);
            //chọn dòng đầu tiên trong Jtable
            if(this.jtDanhSach.getRowCount() > 0) {
                this.jtDanhSach.changeSelection(0, 0, false, false);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    
    public void capNhatHangsx(HangSanXuat hsx) {
        try {
            HangSanXuatDAO.capNhatThongTinHangsx(hsx);
            this.timKiem();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfTenHangsx = new javax.swing.JTextField();
        jbTim = new javax.swing.JButton();
        jbHienThi = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtDanhSach = new javax.swing.JTable();
        jbXoa = new javax.swing.JButton();
        jbCapNhat = new javax.swing.JButton();
        jbLen = new javax.swing.JButton();
        jbXuong = new javax.swing.JButton();
        jbThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("XÓA - CẬP NHẬT HÃNG SX");

        jLabel1.setText("Tên Hãng SX:");

        jtfTenHangsx.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jtfTenHangsxCaretUpdate(evt);
            }
        });
        jtfTenHangsx.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtfTenHangsxActionPerformed(evt);
            }
        });

        jbTim.setText("Tìm");
        jbTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbTimActionPerformed(evt);
            }
        });

        jbHienThi.setText("Hiển Thị Tất Cả");
        jbHienThi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbHienThiActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách hãng sản xuất"));

        jtDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtDanhSach);

        jbXoa.setText("Xóa");
        jbXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbXoaActionPerformed(evt);
            }
        });

        jbCapNhat.setText("Cập Nhật");
        jbCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCapNhatActionPerformed(evt);
            }
        });

        jbLen.setText("Lên");
        jbLen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLenActionPerformed(evt);
            }
        });

        jbXuong.setText("Xuống");
        jbXuong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbXuongActionPerformed(evt);
            }
        });

        jbThoat.setText("Thoát");
        jbThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 442, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbLen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbXuong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 26, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jbXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbCapNhat)
                        .addGap(51, 51, 51)
                        .addComponent(jbLen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbXuong)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbThoat))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jtfTenHangsx, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbTim)
                        .addGap(28, 28, 28)
                        .addComponent(jbHienThi, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfTenHangsx, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbTim)
                    .addComponent(jbHienThi))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbTimActionPerformed
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jbTimActionPerformed

    private void jtfTenHangsxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtfTenHangsxActionPerformed
        // TODO add your handling code here:
        //this.timKiem();
    }//GEN-LAST:event_jtfTenHangsxActionPerformed

    private void jbXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbXoaActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        if(dong >= 0) {
            int kq = JOptionPane.showConfirmDialog(this, "Bạn có thật sự muốn xóa?","Thông báo",JOptionPane.YES_NO_OPTION);
            if(kq == JOptionPane.YES_OPTION) {
                this.xoaHangsx();
            }
        }
    }//GEN-LAST:event_jbXoaActionPerformed

    private void jtfTenHangsxCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jtfTenHangsxCaretUpdate
        // TODO add your handling code here:
        this.timKiem();
    }//GEN-LAST:event_jtfTenHangsxCaretUpdate

    private void jbHienThiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbHienThiActionPerformed
        // TODO add your handling code here:
        this.hienThiTatCaHangsx();
        this.jtfTenHangsx.setText("");
    }//GEN-LAST:event_jbHienThiActionPerformed

    private void jbLenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLenActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        dong = dong -1;
        if(dong >= 0) {
            this.jtDanhSach.changeSelection(dong, 0, false, false);
        } else {
            int dongcuoi = this.jtDanhSach.getRowCount()-1;
            this.jtDanhSach.changeSelection(dongcuoi, 0, false, false);
        }
    }//GEN-LAST:event_jbLenActionPerformed

    private void jbXuongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbXuongActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        dong = dong + 1;
        if(dong < this.jtDanhSach.getRowCount()) {
            this.jtDanhSach.changeSelection(dong, 0, false, false);
        } else {
            this.jtDanhSach.changeSelection(0, 0, false, false);
        }
    }//GEN-LAST:event_jbXuongActionPerformed

    private void jbCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCapNhatActionPerformed
        // TODO add your handling code here:
        int dong = this.jtDanhSach.getSelectedRow();
        if(dong >= 0) {
            //cung cap du lieu cho dialog
            CapNhatHangSanXuat dialog = new CapNhatHangSanXuat(null, true);
            HangSanXuat hsx = new HangSanXuat();
            hsx.setMaHangsx((Integer)this.jtDanhSach.getValueAt(dong, 0));
            hsx.setTenHang((String)this.jtDanhSach.getValueAt(dong, 1));
            dialog.setMaHangsx(hsx.getMaHangsx());
            dialog.setTenHang(hsx.getTenHang());
            dialog.setVisible(true);
            //lay du lieu duoc cap nhat tu dialog
            hsx.setMaHangsx(dialog.getMaHangsx());
            hsx.setTenHang(dialog.getTenHang());
            //cap nhat lại dữ liệu
            this.capNhatHangsx(hsx);
        }
    }//GEN-LAST:event_jbCapNhatActionPerformed

    private void jbThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCapNhat;
    private javax.swing.JButton jbHienThi;
    private javax.swing.JButton jbLen;
    private javax.swing.JButton jbThoat;
    private javax.swing.JButton jbTim;
    private javax.swing.JButton jbXoa;
    private javax.swing.JButton jbXuong;
    private javax.swing.JTable jtDanhSach;
    private javax.swing.JTextField jtfTenHangsx;
    // End of variables declaration//GEN-END:variables
}
